package com.sl.ms.trade.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.sl.ms.trade.annotation.PayChannel;
import com.sl.ms.trade.enums.PayChannelEnum;
import com.sl.transport.common.util.ObjectUtil;

import java.util.Map;

/**
 * @author wjh
 * @create 2023-09-22 14:38
 */
public class HandlerFactory {

    public static <T> T get(PayChannelEnum payChannel, Class<T> nativePayHandlerClass) {
        Map<String, T> payHandlerMap = SpringUtil.getBeansOfType(nativePayHandlerClass);
        for (Map.Entry<String, T> handlerEntry : payHandlerMap.entrySet()) {
            PayChannel annotation = handlerEntry.getValue().getClass().getAnnotation(PayChannel.class);
            if (ObjectUtil.isNotEmpty(annotation) && ObjectUtil.equal(payChannel,annotation.type())) {
                return handlerEntry.getValue();
            }
        }
        return null;
    }
    public static <T> T get(String  payChannel, Class<T> nativePayHandlerClass) {
        return get(PayChannelEnum.valueOf(payChannel),nativePayHandlerClass);
    }
}
